<template >
    <div class="MenuManger">
   <div>

    <Tree :data="data5" :render="renderContent" class="treeStyle"  ></Tree>
        <Modal  v-model="Modal"  >
            <p slot="header">{{modalTitle}}</p>
     <Form :model="form" label-position="left" ref="form" :label-width="100" :rules="ruleValidate">
        <FormItem label="编号" prop="level">
            <Input v-model="form.level"></Input>
        </FormItem>
        <FormItem label="菜单名称" prop="name">
            <Input v-model="form.name" ></Input>
        </FormItem>
        <FormItem label="上级编号" prop="parent_id">
            <Input v-model="form.parent_id"></Input>
        </FormItem>
        <FormItem label="地址" prop="url">
            <Input v-model="form.url"></Input>
        </FormItem>
        <FormItem label="图标" prop="icon">
            <Input v-model="form.icon"></Input>
        </FormItem>
    </Form>
     <div slot="footer">
           <ul>
            <li style="text-align: center;">
            <Button type="info" size="large"  @click="cancel()"  >取消</Button>
       
            <Button type="success" size="large"  v-on:click="append(treeData,'form')"  >保存</Button>
            </li>
            </ul>
        </div>
      </Modal>
   </div>
    

     <Collapse v-model="value1" class="CollapseStyle">
        <Panel  name="pannelName">
         
         
                    <Icon type="log-in" style="margin-left: 5px;" ></Icon>

           
             菜单按钮详情
            
            
        <p slot="content">
        <Table :columns="columns1" :data="tableData" ellipsis=true no-data-text="暂无数据"></Table>
        </p>
        </Panel>
 
    </Collapse>
   
        <!-- <Modal  v-model="Modal"  >
            <p slot="header">{{modalTitle}}</p>
     <Form :model="form" label-position="left" :label-width="100" >
        <FormItem label="编号">
            <Input v-model="form.menuId"></Input>
        </FormItem>
        <FormItem label="菜单名称">
            <Input v-model="form.menuName"></Input>
        </FormItem>
        <FormItem label="上级编号">
            <Input v-model="form.top_Index"></Input>
        </FormItem>
        <FormItem label="地址">
            <Input v-model="form.url"></Input>
        </FormItem>
    </Form>
     <div slot="footer">
           <ul>
            <li style="text-align: center;">
            <Button type="info" size="large"   >取消</Button>
       
            <Button type="success" size="large"   >保存</Button>
            </li>
            </ul>
        </div>
</Modal> -->
 
   </div>
</template>
<script>
    import {postDataToServer} from '../../assets/js/post.js'
    export default {
        data () {
            return {
                value1:1,
                pannelName:1,
                treeData:[],
                tableData: [],
                form:{
                    name:'',
                    level:'',
                    url:'',
                    parent_id:'',
                    icon:''
                },
                menuList:[],
                Modal:false,
                modalTitle:'',
                columns1: [                                     
                    {
                        title: '编号',
                        key: 'level'
                    },
                    {
                        title: '菜单名称',
                        key: 'name'
                    },
                    {
                        title: '上级编号',
                        key: 'parent_id'
                    },
                    {
                        title: '地址',
                        key: 'url'
                    },
                    {
                        title: '菜单图标',
                        key: 'icon'
                    },
                    {
                        title: 'Action',
                        key: 'action',
                        width: 250,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.show(params.row)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                     style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.delate(params.row)
                                        }
                                    }
                                }, '删除'),
                                h('Button', {
                                    props: {
                                        type: 'success',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.add(params.row)
                                        }
                                    }
                                }, '新增')
                            ]);
                        }
                    }
                ],
                
                data5: [
                    {

                        title: '菜单管理',
                        expand: true,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h('Icon', {
                                        props: {
                                            type: 'navicon-round'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }

                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '32px'
                                    }
                                }, [
                                    h('Button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'ios-plus-empty',
                                            type: 'primary'
                                        }), 
                                        style: {
                                            width: '52px'
                                        },
                                        on: {
                                            click: () => { this.getTreeData(data) }
                                        }
                                    })
                                ])
                            ]);
                        },
                        children: [
                            {
                                title: '申请实验室',
                                expand: true,
                                children: []
                            },
                            {
                                title: '任务周志',
                                expand: true,
                                children: []
                            },
                            {
                                title:'技术日志',
                                expand:true,
                                children:[]
                            },
                            {
                                title:'菜单管理',
                                expand:true,
                                children:[]
                            }
                        ]
                    }
                ],
                buttonProps: {
                    type: 'Ghost',
                    size: 'small',
                },
                ruleValidate:{
                    name:[
                      {required:true, message: '菜单名称不能为空',trigger: 'blur' }
                    ],
                    level:[
                      {required:true, message: '菜单编号不能为空',trigger: 'blur' }
                    ],
                    parent_id:[
                       {required:false, message: '上级编号不能为空',trigger: 'blur' },
                       {min:0,max:3,message: '上级编号不能为空',trigger: 'blur'}
                    ],
                    url:[{
                        required:true, message: '菜单地址不能为空',trigger: 'blur'
                    }],
                    icon:[{
                        required:true, message: '图标不能为空',trigger: 'blur'
                    }]
                }
            }
        },
        methods:{
            show(row){
                this.modalTitle=row.menuName;
                this.Modal=true;
            },
            delate(index){
                let postData = new Object();
                let parameter = new Object();
                postData.url = 'MENU_DELATE';
                parameter.id = index.id;
                postData.parameter = parameter;
                postDataToServer(postData).then((data)=>{
                    console.log(data);
                });
                 this.getListPage();

            },
            add(row){
                this.modalTitle=row.menuName;
                this.Modal=true; 
            },

            renderContent (h, { root, node, data }) {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: 'drag'
                            },
                            style: {
                                marginRight: '15px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px'
                        }
                    }, [
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-plus-empty'
                            }),
                            style: {
                                marginRight: '8px'
                            },
                            on: {
                                click: () => { this.getTreeData(data) }
                            }
                        }),
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-minus-empty'
                            }),
                            on: {
                                click: () => { this.remove(root, node, data) }
                            }
                        })
                    ])
                ]);
            },
            getTreeData(data){
                this.Modal=true;
                this.treeData=data;
                this.form.name='';
                this.form.level='';
                this.form.url='';
                this.form.parent_id='';
                this.form.icon='';
            },
            cancel(){
                this.Modal=false;
            },
            save(){
                let postData = new Object();
                let parameter = new Object();
                postData.url = 'MENU_ADD';
                parameter = this.form;
                postData.parameter = parameter;
                postDataToServer(postData).then((data)=>{
                    console.log(data);
                });
            },
            append (data,name) {
               this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.save();
                        this.$Message.success('Success!');
                    
                const children = data.children || [];
                children.push({
                    title: this.form.name,
                    expand: true
                });
                this.$set(data, 'children', children);
                this.Modal=false;
                 this.getListPage();
                } else {
                        this.$Message.error('Fail!');
                    }
                })
                 
            },
            remove (root, node, data) {
                const parentKey = root.find(el => el === node).parent;
                const parent = root.find(el => el.nodeKey === parentKey).node;
                const index = parent.children.indexOf(data);
                parent.children.splice(index, 1);
            },
            getListPage(){
            let _this = this;
            let postData = new Object();
            let parameter = new Object();
            postData.url = 'MENU_LISTPAGE';
            postDataToServer(postData).then(function(data){
                console.log(data);
                _this.tableData= data.data.rows;
            })
            }


        },
        created(){
            this.getListPage();
        
        }
    }
</script>
<style scoped>
    .formStyle{
        width: 50%;
        margin-left: 300px;
    }
    .treeStyle{
        margin-left: 40px;
    }
    .CollapseStyle{
     background:rgba(0,0,0,0);
        position: relative;
        border: 0px solid red;
        margin-left: 10px;

    }
    .MenuManger{
         height: 100%;
         width: 100%;
    }   
</style>